<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="box">
        <router-link to="/home">主页</router-link>
        <router-link to="/company/123">产品详情</router-link>
        <router-link to="/company/456">小猪煤气</router-link>
        <router-view></router-view>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vue-router/dist/vue-router.js"></script>
<script>

    let routes = [
        {
            path: "/home",
            beforeRouteEnter(to,from,next) {
                console.log("/home 导航守卫");
                next();
            },
            component: () => Promise.resolve({
                template: `<h1>主页</h1>`,
                beforeRouteEnter(to, from, next) {
                    console.log("进入主页的路由");
                    console.log(this);//undefind 现在还不能访问到实例
                    next();
                },
                //路由更新
                beforeRouteUpdate(to, from, next) {
                    console.log("路由更新的时候");
                    console.log(this);
                    next();
                },
                beforeRouteLeave(to, from, next) {
                    console.log("导航守卫 离开home主页");
                    console.log("当前路由", from);
                    console.log("前往路由", to);
                    next();
                }
            })
        },
        {
            path: "/company/:id",
            component: () => import("./company.js")
        }
    ]
    let router = new VueRouter({
        routes
    })
    router.beforeEach((to, from, next) => {
        console.log("执行全局导航守卫");
        next();
    })
    let ve = new Vue({
        router,
        el: "#box",
    })

</script>

</html>